<template>
  <div class="vlog">
    <div class="search">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="">
          <el-input
            v-model="formInline.username"
            placeholder="用户账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-select v-model="formInline.type" placeholder="角色类型">
            <el-option label="管理员" :value="1"></el-option>
            <el-option label="普通用户" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="formInline.time"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="box">
      <el-tabs type="border-card">
        <el-tab-pane label="登录日志">
          <div class="table">
            <el-table :data="tableData" style="width: 100%">
              <el-table-column
                prop=""
                align="center"
                type="index"
                label="序号"
                width="100px"
              >
              </el-table-column>
              <el-table-column
                align="center"
                prop="username"
                label="登录账号"
                width=""
              >
              </el-table-column>
              <el-table-column align="center" prop="type" label="角色类型">
                <template slot-scope="scope">
                  <span v-if="scope.row.type == 1">管理员</span>
                  <span v-if="scope.row.type == 2">普通用户</span>
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="email"
                label="邮箱"
                width="180"
              >
              </el-table-column>
              <el-table-column
                align="center"
                prop="createtime"
                label="登录时间"
                width=""
              >
                <template slot-scope="scope">
                  <span>{{
                    scope.row.createtime.split("T")[0] +
                    " " +
                    scope.row.createtime.split("T")[1].split(".")[0]
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column align="center" prop="" label="操作" width="">
                <template slot-scope="scope">
                  <el-button
                    type="danger"
                    @click="clear(scope.row, scope.$index)"
                    >删除记录</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="page" style="text-align: right">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageNumber"
              :page-sizes="[10, 20, 30, 40]"
              :page-size="pageSize"
              background=""
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
            >
            </el-pagination>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //分页
      pageSize: 10,
      pageNumber: 1,
      total: 0,
      //表格数据
      tableData: null,
      //查询表单
      formInline: {
        username: "",
        type: "",
        time: "",
        // currPage: 0
        // pageNum: 9
      },
    };
  },
  methods: {
    //删除单行数据
    clear(row, index) {
      this.$confirm("确定删除该条登录记录?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .get("users/delLoginData", {
              params: {
                id: row.id,
                token: sessionStorage.getItem("token"),
              },
            })
            .then((res) => {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getTableData();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //初始化表格数据
    getTableData() {
      this.$axios
        .get("users/getLoginData", {
          params: {
            username: "",
            type: "",
            startDate: "",
            endDate: "",
            currPage: this.pageNumber - 1,
            pageNum: this.pageSize,
          },
        })
        .then((res) => {
          this.tableData = res.data.data;
          this.total = res.data.total;
        });
    },
    //分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNumber = val;
      this.getTableData();
    },
    //查询
    onSearch() {
      this.$axios
        .get("users/getLoginData", {
          params: {
            username: this.formInline.username,
            type: this.formInline.type,
            startDate: this.formInline.time[0],
            endDate: this.formInline.time[1],
            currPage: this.pageNumber,
            pageNum: this.pageSize,
          },
        })
        .then((res) => {
          this.tableData = res.data.data;
          this.total = res.data.total;
        });
    },
  },
  created() {
    this.getTableData();
  },
};
</script>

<style lang="scss" scoped>
.vlog::v-deep {
  .box {
    .table {
      thead {
        tr {
          th {
            color: black;
            background-color: #ddd;
            font-size: 16px;
          }
        }
      }
    }
  }
}
</style>